package com.lianqi.emcpframework.logging.aop;

import java.lang.reflect.Method;

import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.StringUtils;

import com.lianqi.emcpframework.core.boost.aop.MethodInterceptorHolder;
import com.lianqi.emcpframework.logging.context.LoggerDefinition;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @program emcp-framework
 * @ClassName SwaggerLoggerParser
 * @description: 有swagger注解的类或方法调用日志注解解析器
 * @author: sky
 * @create: 2018/11/17 23:35
 */
public class SwaggerLoggerParser implements InvokingLoggerParser {
    @Override
    public boolean support(Class clazz, Method method) {
        Api api = AnnotationUtils.findAnnotation(clazz, Api.class);
        ApiOperation operation = AnnotationUtils.findAnnotation(method, ApiOperation.class);
        return api != null || operation != null;
    }

    @Override
    public LoggerDefinition parse(MethodInterceptorHolder holder) {
        Api api = holder.findAnnotation(Api.class);
        ApiOperation operation = holder.findAnnotation(ApiOperation.class);
        String action = "";
        if (api != null) {
            action = action.concat(api.value());
        }
        if (null != operation) {
            action = StringUtils.isEmpty(action) ? operation.value() : action.concat("-").concat(operation.value());
        }
        return new LoggerDefinition(action, "", "");

    }
}
